package com.tsp_tux.demo.Controller;

import com.tsp_tux.demo.Entity.RespBean;
import com.tsp_tux.demo.Entity.SubjectInfo_1;
import com.tsp_tux.demo.Entity.Subject_User;
import com.tsp_tux.demo.Service.SubjectService;
import com.tsp_tux.demo.Util.easyRespBean;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
public class SubjectController {

    @Resource
    SubjectService subjectService;

    /**
     * 新增专题
     */
    @PostMapping("/createSubject")
    RespBean creatureSubject(@RequestParam("uid") String uid,
                             @RequestParam("content") String content,
                             @RequestParam("subName") String subName) {
        return subjectService.createSubject(uid, content, subName);
    }

    /**
     * 随机推荐六个专题
     */
    @GetMapping("/recommendSubject")
    RespBean getRandom6Subjects() {
        return subjectService.getRandom6subjects();
    }

    /**
     * 查看所有/自己的专题列表
     *
     * @param uid 0为所有专题，id为教师自己发布的专题
     * @return 状态码
     * @author 张志豪
     */
    @GetMapping("/checkSubjectList")
    public RespBean checkSubjectList(@RequestParam(value = "uid",defaultValue = "0")String uid) {
        return subjectService.checkSubjectList(uid);
    }

    /**
     * 查看专题中的内容
     *
     * @param sid    专题id
     * @param status 0学生发布的内容，1教师发布的内容
     * @return 查询信息和结果
     * @author 张志豪
     */
    @PostMapping("/checkSubject")
    public RespBean checkSubject(@RequestParam(value = "sid", defaultValue = "-1") int sid,
                                 @RequestParam(value = "status", defaultValue = "1") int status,
                                 @RequestParam(value = "atype",defaultValue = "0")int atype) {
        return subjectService.checkSubject(sid, status,atype);
    }

    /**
     * 删除一个专题
     *
     * @param sid
     */
    @PostMapping("/deleteSubject")
    public RespBean deleteSubject(int sid) {
        subjectService.deleteSubject(sid);
        return easyRespBean.ok();
    }

    @GetMapping("checkStudent")
    public RespBean checkStudent(int sid) {
        return easyRespBean.ok(subjectService.checkStudent(sid));
    }

    @PostMapping("/joinSubject")
    public RespBean joinSubject(String uid, int sid) {
        return subjectService.joinSubject(uid, sid);
    }

    @PostMapping("/exitSubject")
    public RespBean exitSubject(String uid, int sid){
        return subjectService.exitSubject(uid, sid);
    }

    @PostMapping("/inSubject")
    public RespBean inSubject(String uid, int sid){
        return subjectService.inSubject(uid, sid);
    }
    /**
     * 获取专题页详情
     * @author 张志豪
     * @param sid 专题id
     * @return 专题详情
     */
    @GetMapping("getSubjectDetails")
    public RespBean getSubjectDetails(@RequestParam("sid")int sid){
        return subjectService.getSubjectDetails(sid);
    }

    /**
     * 关键词模糊搜索专题
     *
     * @param keyword    关键字
     * @return 查询信息和结果
     * @author 童一凡
     */
    @PostMapping("/searchBlogs")
    @ResponseBody
    @CrossOrigin
    public RespBean searchBlogs(String keyword){
        String[] temp=keyword.split(" ");
        List<String> sid=new ArrayList<>();
        for(int i=0;i<temp.length;i++){
            List<String> searchB_Key=subjectService.searchBlogByKeyword(temp[i]);
            if(searchB_Key!=null&& !searchB_Key.isEmpty()){
                sid.addAll(searchB_Key);
            }
        }
        if( sid==null||sid.size()==0){
            return new RespBean(404, "专题不存在", null);
        }
        else {
            HashMap<String, Integer> hMap = new HashMap<>();
            for (String temp1 : sid) {
                if (hMap.get(temp1) == null) {
                    hMap.put(temp1, 1);
                } else {
                    hMap.put(temp1, hMap.get(temp1) + 1);
                }
            }
            HashMap<String, Integer> finalOut = new LinkedHashMap<>();
            hMap.entrySet()
                    .stream()
                    .sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue()))
                    .collect(Collectors.toList()).forEach(ele -> finalOut.put(ele.getKey(), ele.getValue()));
            List<String> temp2 = new ArrayList<String>();
            for (Map.Entry<String, Integer> temp3 : finalOut.entrySet()) {
                temp2.add(temp3.getKey());
            }
            List<SubjectInfo_1> search_listB = new ArrayList<>();
            for (int i = 0; i < temp2.size(); i++) {
                SubjectInfo_1 search_ListB = subjectService.searchBlogByKeyword2(temp2.get(i));
                search_listB.add(search_ListB);
            }
            if (search_listB.isEmpty()) {
                return new RespBean(404, "专题不存在", null);
            } else
                return new RespBean(200, "Success!", search_listB);
        }
    }

    /**
     * 获取专题统计数据
     * return data::num	int[5]	[专题参与人数,专题教师文章数,专题资源数,学生知识分享数,学生心得数目]
     * @author 周志伟
     */
    @GetMapping("/getSubjectNumbers")
    public RespBean getSubjectNumbers(int sid){
        return new RespBean(200, "查询成功", subjectService.getSubjectNumbers(sid));
    }


}